package Dao;

import bean.customer;

import java.sql.SQLException;
import java.util.List;

//Object 是 Java 类层次结构中的顶级类。
// 每个 Java 对象都是一个 Object 类型对象。Object 类提供了一些通用的方法，如 equals()、hashcode()和toString()。

public class customerDaoIpl extends BaseDao implements customerDao{
    @Override
    public int addCustomer(customer customer) {
        String sql = "INSERT INTO customer (name, phonenumber, password) VALUES(?,?,?)";
        try {
            return update(sql,customer.getName() ,customer.getPhoneNumber(),customer.getPassword());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int deleteCustomerBYId(Integer id) {
        String sql = "DELETE FROM customer WHERE pid=?";
        try {
            return update(sql,id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int deleteCustomerBYPhoneNumber(String phoneNumber) {
        String sql = "DELETE FROM customer WHERE phonenumber=?";
        try {
            return update(sql,phoneNumber);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int updateCustomer(customer customer) {
        String sql = "update customer set name = ?, password = ?,phonenumber =  ? where pid = ? ";
        try {
            return update(sql,customer.getName(),customer.getPassword(),customer.getPhoneNumber(),customer.getPid());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public customer queryCustomerById( Integer id) {
        String sql = "SELECT pid,name,phonenumber,password FROM customer WHERE pid = ?";
        try {
            return queryForOne(customer.class,sql,id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Integer queryIdByPhoneNumber(String phonenumber) throws Exception {
        String sql = "select pid from customer where phonenumber = ?";
        return (Integer) queryForSingleValue(sql,phonenumber);
    }

    @Override
    public customer queryCustomerByPhoneNumber( String phonenumber) {
        String sql = "SELECT pid, name,phonenumber,password FROM customer WHERE phonenumber = ?";
        try {
            return queryForOne(customer.class,sql,phonenumber);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }





    @Override
    public List<customer> queryCustomers() throws Exception {
        String sql = "SELECT pid,name,phonenumber,password FROM customer";
        return queryForList(customer.class,sql);
    }



}